import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp();

Page({
    data: {
        member: 0,
        show: false,
        columns: ['未分组', '一组', '二组', '三组', '四组', '观察组', '战术小队'],
        userGroup: '未分组',
        userId: ''
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("sign-in")
            .where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
    },
    async delete(e) {
        this.setData({ show: true });
        this.setData({ userId: e.target.dataset.id });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    onClose() {
        this.setData({ show: false });
    },
    onChange(event) {
        const { picker, value, index } = event.detail;
        this.setData({ userGroup: value });
    },
    onClickLeft() {
        this.setData({ show: false });
    },
    async onClickRight() {
        this.setData({ show: false });
        const db = qq.cloud.database();
        const _ = db.command;
        let admin = false;
        const This = this;
        await db.collection('administer').where({
            unionId: _.eq(app.data.unionId)
        }).get().then((res) => {
            if (res.data.length != 0) {
                admin = true
            }
        })
        if (admin) {
            db.collection("sign-in")
                .where({
                    unionId: _.eq(this.data.userId),
                    alliance: app.data.alliance
                })
                .update({
                    data: {
                        "group": _.set(this.data.userGroup)
                    }
                })
                .then(res => {
                    Toast('修改成功');
                    db.collection("sign-in")
                        .where({
                            alliance: app.data.alliance
                        })
                        .get()
                        .then(res => {
                            This.setData({ member: res.data });
                        });
                })
                .catch(err => {
                    Toast('修改失败');
                })
        } else {
            Toast('没有管理员权限');
        }

    },
})